package sangpil.presentation
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import gs.TweenLite;
	import gs.easing.Cubic;
	
	import sangpil.events.GeneralEvent;

	public class DotsStage extends Sprite
	{
		private var dotsArr:Array = new Array();
		private var plate:LargeCircle;
		
		public function DotsStage()
		{
			super();
			this.setLayout();
			this.addEvent();
			this.setDefault();
		}
		
		private function setLayout():void
		{
			this.plate = new LargeCircle();
			this.plate.strtxt.alpha = 0;
		}
		
		private function addEvent():void
		{
			this.addMouseEvent();
			this.addEventListener(Event.ADDED_TO_STAGE, addKeyboardEvent);
			//this.plate.circumstance.alpha = 0.7;
			this.plate.circumstance.addEventListener(Event.ENTER_FRAME, rotating);
		}
		
		private function rotating(e:Event):void
		{
			e.target.rotation += 0.1;
		}
		
		private function addMouseEvent():void
		{
			this.plate.ComputationalDesign.addEventListener(MouseEvent.MOUSE_DOWN, addDot);
			this.plate.circumstance.addEventListener(MouseEvent.MOUSE_DOWN, allUp);
			this.plate.circumstance.addEventListener(MouseEvent.MOUSE_UP, allDown);
		}
		
		private var devArr:Array;
		private var undevArr:Array;
		private function allUp(e:Event):void
		{
			//trace(this.numChildren);
			this.devArr = null;
			this.undevArr = null;
			this.devArr = [];
			this.undevArr = [];
			for(var i:uint = 0; i < this.numChildren;i++)
			{
				if(i != 0)
				{
					var dot:Dot = this.getChildAt(i) as Dot;
					if(dot.developed)
					{
						dot.unZipDevShape();
						this.devArr.push(dot);
					}else{
						this.undevArr.push(dot);
					}
				}
			}
			
			
			this.goCenter();
			this.goOutter();
			
		}
		
		private function goCenter():void
		{
			var r:Number = (this.devArr.length * 30) / Math.PI*2;
			for(var i:uint = 0; i < this.devArr.length; i++)
			{
				TweenLite.to(this.devArr[i], 0.7, {
													ease:Cubic.easeInOut, 
													x:Math.sin(Math.PI*2/this.devArr.length*i)*r,
													y:Math.cos(Math.PI*2/this.devArr.length*i)*r});
			}
		}
		
		private function goOutter():void
		{
			var r:Number = this.plate.ComputationalDesign.width/2 - 60;
			for(var i:uint = 0; i < this.undevArr.length; i++)
			{
				TweenLite.to(this.undevArr[i], 0.7, {
													ease:Cubic.easeInOut, 
													x:Math.sin(Math.PI*2/this.undevArr.length*i)*r,
													y:Math.cos(Math.PI*2/this.undevArr.length*i)*r});
			}
		}
		
		private function allDown(e:Event):void
		{
			for(var i:uint = 0; i < this.numChildren;i++)
			{
				if(i != 0)
				{
					var dot:Dot = this.getChildAt(i) as Dot;
					if(dot.developed)
					{
						dot.zipDevShape();
					}
				}
			}
			this.dotGoBack();
		}
		
		private function dotGoBack():void
		{
			for(var i:uint = 0; i < this.devArr.length; i++)
			{
				var dDot:Dot = this.devArr[i] as Dot;
				TweenLite.to(dDot, 0.7, {
										ease:Cubic.easeInOut, 
										x:dDot.orgPosX,
										y:dDot.orgPosY})
			}
			for(var j:uint = 0; j < this.undevArr.length; j++)
			{
				var nDot:Dot = this.undevArr[j] as Dot;
				TweenLite.to(nDot, 0.7, {
										ease:Cubic.easeInOut, 
										x:nDot.orgPosX,
										y:nDot.orgPosY})
			}
		}
		
		private function addDot(e:MouseEvent):void
		{
			var temp:Dot = new Dot(30, 0xBDD684);
			temp.x = this.mouseX;
			temp.y = this.mouseY;
			this.addChild(temp);
			
			temp.orgPosX = temp.x;
			temp.orgPosY = temp.y;
			
			temp.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
			temp.addEventListener(MouseEvent.MOUSE_UP, onUp);
			temp.addEventListener(GeneralEvent.CHANGED, showSelectedStr);
			
			this.dotsArr.push(temp);
			
			this.plate.strtxt.text = 'Idea Added '+(this.numChildren-1)+'th';
			TweenLite.to(this.plate.strtxt, 0.5, {ease:Cubic.easeInOut, alpha:1, onComplete:txtAniEnd});
		}
		
		private function showSelectedStr(e:GeneralEvent):void
		{
			this.plate.strtxt.text = e.str;
			TweenLite.to(this.plate.strtxt, 0.5, {ease:Cubic.easeInOut, alpha:1, onComplete:txtAniEnd});
		}
		
		private var startPoint:Point;
		private var selectedDot:Dot;
		private function onDown(e:MouseEvent):void
		{
			var dot:Dot = e.currentTarget as Dot;
			this.startPoint = null;
			this.startPoint = new Point(dot.x, dot.y);
			
			this.selectedDot = dot;
			
			dot.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
			dot.addEventListener(MouseEvent.MOUSE_OUT, onOut);
		}
		
		private function onUp(e:MouseEvent):void
		{
			var dot:Dot = e.currentTarget as Dot;
			dot.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);
			dot.removeEventListener(MouseEvent.MOUSE_OUT, onOut);
			dot.orgPosX = this.mouseX;
			dot.orgPosY = this.mouseY;
		}
		
		private function onMove(e:MouseEvent):void
		{
			var dot:Dot = e.currentTarget as Dot;
			dot.x = this.mouseX;
			dot.y = this.mouseY;
		}
		
		private function onOut(e:MouseEvent):void
		{
			var dot:Dot = e.currentTarget as Dot;
			dot.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);
			dot.removeEventListener(MouseEvent.MOUSE_OUT, onOut);
			dot.orgPosX = this.mouseX;
			dot.orgPosY = this.mouseY;
		}
		
		private function txtAniEnd():void
		{
			TweenLite.to(this.plate.strtxt, 0.5, {delay:1, ease:Cubic.easeInOut, alpha:0});
		}
		
		private function addKeyboardEvent(e:Event):void
		{
			//잠시.. 바탕에 들어갔을때 스테이지 정보를 들여오기 위해.
			
			//가운데로 이동.
			this.addChild(this.plate);
		}
		
		private function setDefault():void
		{
			
			
		}
		
	}
}